<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Session;
use \think\Db;
class Api extends Controller
{
	public function login()
    {	
		//验证用户名密码
		$array = array();
		if(!request()->isPost()){
			$array=[
						'userid'=>0,
						'message'=>'error'
					];
					echo json_encode($array,JSON_UNESCAPED_UNICODE);
					return ;
		}
		$a=input('username');
		$p=input('password');
		$admin=Db::name('user')->where('user_loginname',$a)->where('user_isdel',0)->join('city','user.city_id=city.city_id')->find();
		if($admin&&$a!='管理员'){
				if(md5($p)==$admin['user_password']){
					$array=[
						'userid'=>$admin['user_id'],
						'cityid'=>$admin['city_id'],
                        'cityname'=>$admin['city_name'],
						'message'=>'success'
					];
					echo json_encode($array,JSON_UNESCAPED_UNICODE);
				}else{
					//echo "<script>alert('密码错误');</script>";
					$array=[
						'userid'=>0,
						'cityid'=>0,
						'message'=>'密码错误'
					];
					echo json_encode($array,JSON_UNESCAPED_UNICODE);
				}
			}else{
				//echo "<script>alert('用户名不存在');</script>";
				$array=[
						'userid'=>0,
						'cityid'=>0,
						'message'=>'用户不存在'
					];
					echo json_encode($array,JSON_UNESCAPED_UNICODE);
			}
    }

    //city username loginname password gender tel captchacode captchaid
    public function reg(){
    	$resultarray = array();
    	if(request()->isPost()){
    		$u = input('loginname');
    		$t = input('email');
    		$user = Db::name('user')->where('user_name',$u)->find();
    		$tel = Db::name('user')->where('user_email',$t)->find();
    		if($user){
    			$resultarray=[
    				'message'=>'该用户名已存在'
    			];
    			//VZIOS
    		}else if($tel){
    			$resultarray=[
    				'message'=>'该邮箱已存在'
    			];
    		}else{
    			$checkcode = input('captchacode');
    			$checkid = input('captchaid');
    			$r = Db::name('captcha')->where('captcha_id',$checkid)->find();
    			if(!($r['captcha_code']==$checkcode)){
    				$resultarray=[
    					'message'=>'验证码输入错误'
    				];
    			}else{
	    			$array = array();
	    			$array = [
	    				'city_id'=>input('city'),
	    				'user_name'=>input('username'),
	    				'user_loginname'=>$u,
	    				'user_password'=>md5(input('password')),
	    				'user_gender'=>input('gender'),
	    				'user_email'=>input('email'),
	    				'user_intro'=>'这个人什么都没有留下',
	    				'user_regtime'=>date('Y-m-d H:i:s'),
	    				'user_headimage'=>'default.jpg',
	    				'user_iswechat'=>0,
	    				'user_wechatcode'=>0,
	    				'user_isdel'=>0,
	    			];
	    			$result = Db::name('user')->insert($array);
	    			if($result){
	    				$resultarray=[
	    					'message'=>'success'
	    				];
	    			}else{
	    				$resultarray=[
	    					'message'=>'sql error'
	    				];
	    			}
    		}
    			
    		}
    	}else{
    		$resultarray=[
    				'message'=>'error'
    			];
    	}
    	echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }



    public function getcitylist(){
    	$resultarray = Db::name('city')->order('city_id')->where('city_status',1)->select();
    	echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }


    //获取社区列表
    public function getfocuscommunitylist(){
    	$resultarray = Db::name('community')->order('community_id')->where('community_status',1)->select();
    	echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getcommunitylist(){
        $sql = "SELECT a.community_id,a.community_name,a.community_pic,ifnull(b.count,0)as count FROM `community`as `a` left join (select community_id,count(*)as count from `communityfocus` group by community_id)`b` on `a`.community_id = `b`.community_id";

    	$resultarray = Db::query($sql);
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function focuscommunity(){
    	$resultarray = array();
        $data = [
            'community_id'=>input('communityid'),
            'user_id'=>input('userid'),
            'focus_time'=>date('Y-m-d H:i:s')
        ];
        $result = Db::name('communityfocus')->insert($data);
        if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function unfocuscommunity(){
    	$resultarray = array();
        $result = Db::name('communityfocus')->where('community_id',input('communityid'))->where('user_id',input('userid'))->delete();
        if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function isfocuscommunity(){
    	$resultarray = array();
        $result = Db::name('communityfocus')->where('community_id',input('communityid'))->where('user_id',input('userid'))->find();
        if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getmessagelist(){
    	$userid = input('userid');
		
		$sql = 'SELECT p.msg_id,p.send_user,p.receive_user,p.user_id2,count(p.msg_id)count,count(case when p.msg_status=1 then p.msg_status else null end)count2,msg_content,msg_time,u.user_name,u.user_headimage FROM (select * from message where user_id = '.$userid.' order by msg_id desc) p inner join user u on u.user_id=p.user_id2 group by p.user_id2 order by p.msg_id desc';
		$resultarray = Db::query($sql);
		echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getmessage(){
    	$user1 = input('userid1');
		$user2 = input('userid2');
		$sql = 'SELECT p.msg_id,p.msg_content,p.send_user,p.receive_user,p.msg_time,p.msg_type,u.`user_name` AS sender_name,u.user_headimage AS sender_image,uu.`user_name` AS receiver_name,uu.`user_headimage` AS receiver_image FROM message p INNER JOIN user u on u.user_id=p.send_user INNER JOIN user uu on uu.user_id=p.receive_user WHERE p.user_id='.$user1.' and p.user_id2= '.$user2.' and p.`msg_status` !=3 ORDER BY p.msg_id';
		$resultarray = Db::query($sql);
        //设为已读
        Db::name('message')->where('user_id',$user1)->where('user_id2',$user2)->update(['msg_status'=>2]);
		echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    //type 1文字 2图片
    public function sendmessage(){
    	$user1 = input('userid1');
		$user2 = input('userid2');
		$msg_content = input('content');
		if(input('type')==2){
		  $file = request()->file('pic');
          if($file){
              $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
              $msg_content = $info->getFilename();
			}
		}
		
		$data = [
			[
				'user_id'=>$user1,
				'user_id2'=>$user2,
				'send_user'=>$user1,
				'receive_user'=>$user2,
				'msg_type'=>input('type'),
				'msg_content'=>$msg_content,
				'msg_time'=>date('Y-m-d H:i:s'),
				'msg_status'=>2,
			],
			[
				'user_id'=>$user2,
				'user_id2'=>$user1,
				'send_user'=>$user1,
				'receive_user'=>$user2,
				'msg_type'=>input('type'),
				'msg_content'=>$msg_content,
				'msg_time'=>date('Y-m-d H:i:s'),
				'msg_status'=>1,
			]
		];
		$result = Db::name('message')->insertAll($data);
		if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function delmessage(){
    	$result = Db::name('message')->where('user_id',input('userid1'))->where('user_id2',input('userid2'))->update(['msg_status'=>3]);
    	if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function watchmessage(){
    	$result = Db::name('message')->where('user_id',input('userid1'))->where('user_id2',input('userid2'))->update(['msg_status'=>2]);
    	if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function upmoment(){
        $type = input('type');
        $pic = "";
        $video = "";
        if($type == 2){
            $files = request()->file('image');
            $i = 0;
            foreach($files as $file){
                $info = $file->validate(['size'=>314572800000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters',md5(microtime(true)).$i);
                $pic = $pic.$info->getFilename().',';
                $i++;
            }
        }else if($type == 3){
            $v = request()->file('video');
            $videoinfo = $v->rule('uniqid')->validate(['size'=>314572800000000,'ext'=>'mp4,avi,wmv,flv'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'videos');
            $video = $videoinfo->getFilename();
            $pic = "black.jpg,";
        }else if($type == 4){
            $files = request()->file('image');
            $i = 0 ;
            foreach($files as $file){
                $info = $file->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters',md5(microtime(true)).$i);
                $pic = $pic.$info->getFilename().',';
                $i++;
            }
            $v = request()->file('video');
            $videoinfo = $v->rule('uniqid')->validate(['size'=>314572800000000,'ext'=>'mp4,avi,wmv,flv'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'videos');
            $video = $videoinfo->getFilename();
        }
        $data = [
            'user_id'=>input('userid'),
            'moment_content'=>input('content'),
            'moment_pic'=>$pic,
            'moment_video'=>$video,
            'moment_time'=>date('Y-m-d H:i:s'),
            'moment_like'=>0,
            'moment_comment'=>0,
            'comment_status'=>1,
            'moment_status'=>1,
        ];
        $result = Db::name('moment')->insert($data);
        if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    //all
    //只能看同城的
    public function getmomentlist(){
        $resultarray = Db::name('moment')->order('moment_id desc')->alias('a')->join('user b','a.user_id=b.user_id')->where('b.city_id',input('cityid'))->where('moment_status',1)->where('b.user_isdel',0)->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getusermomentlist(){
        $resultarray = Db::name('moment')->order('moment_id desc')->alias('a')->join('user b','a.user_id=b.user_id')->where('a.user_id',input('userid'))->where('moment_status',1)->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    //focus
    public function getmomentlistforfocus(){
        $id = input('userid');
        $city = input('cityid');
        $sql = "SELECT * FROM (moment as a,(select user_fid as user_id from `userfocus` where user_id = ".$id.")b) INNER JOIN `user` `c` ON `a`.`user_id`=`c`.`user_id` WHERE  (  a.user_id=b.user_id and a.moment_status = 1 and `c`.`city_id`=".$city.") ORDER BY moment_id desc";
        $resultarray = Db::query($sql);
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getmymomentlist(){
        $resultarray = Db::name('moment')->order('moment_id desc')->where('a.user_id',input('userid'))->alias('a')->join('user b','a.user_id=b.user_id')->where('moment_status',1)->select();
        //设为已读
        Db::name('moment')->where('user_id',input('userid'))->update(['comment_status'=>0]);
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getmoment(){
        $resultarray = Db::name('moment')->where('moment_id',input('momentid'))->alias('a')->join('user b','a.user_id=b.user_id')->find();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    //用户暂无更改能力
    public function updatemoment(){

    }

    //用户无恢复能力
    public function delmoment(){
        $result = Db::name('moment')->where('moment_id',input('momentid'))->update(['moment_status'=>2]);
        if($result){
            Db::name('momentcomment')->where('moment_id',input('momentid'))->update(['comment_status'=>2]);
             Db::name('momentlike')->where('moment_id',input('momentid'))->delete();
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    //isreply = 1 ：其他用户评论，存入该用户id，更改moment评论状态，moment评论+1
    //isreply = 2 ：发布用户回复，存入被回复用户id，更改moment评论状态，moment评论+1
    public function upmomentcomment(){
        $isreply = input('isreply');
        $data = [
            'moment_id'=>input('momentid'),
            'user_id'=>input('userid'),
            'comment_content'=>input('content'),
            'comment_pic'=>input('pic'),
            'comment_time'=>date('Y-m-d H:i:s'),
            'comment_isreply'=>$isreply,
            'comment_replystatus'=>1,
            'comment_status'=>1
        ];
        $result = Db::name('momentcomment')->insert($data);
        if($result){
            //评论数+1
            Db::name('moment')->where('moment_id',input('momentid'))->inc('moment_comment')->update();
            //moment评论状态改变
            Db::name('moment')->where('moment_id',input('momentid'))->update(['comment_status'=>1]);
            $resultarray=[
                    'message'=>'success'
            ];
        }else{
            $resultarray=[
                        'message'=>'error'
                    ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function delmomentcomment(){
        $result = Db::name('momentcomment')->where('comment_id',input('commentid'))->update(['comment_status'=>2]);
        if($result){
            $result2 = Db::name('moment')->where('moment_id',input('momentid'))->dec('moment_comment')->update();
            if($result2){
                if($result2){
            $resultarray=[
                    'message'=>'success'
                ];
            }else{
                $resultarray=[
                        'message'=>'error'
                    ];
                }
            }
        }else{
            $resultarray=[
                        'message'=>'error'
                    ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    //如果comment_isreply为2  则userid表示为被回复人的id
    public function getmomentcomment(){
        $resultarray = Db::name('momentcomment')->where('moment_id',input('momentid'))->where('comment_status',1)->alias('a')->join('user b','a.user_id = b.user_id')->where('comment_status',1)->order('comment_id')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function likemoment(){
        $data = [
            'moment_id'=>input('momentid'),
            'user_id'=>input('userid'),
            'like_time'=>date('Y-m-d H:i:s'),
        ];
        $result = Db::name('momentlike')->insert($data);
        if($result){
            $result2 = Db::name('moment')->where('moment_id',input('momentid'))->inc('moment_like')->update();
            if($result2){
                if($result2){
            $resultarray=[
                    'message'=>'success'
                ];
            }else{
                $resultarray=[
                        'message'=>'error'
                    ];
                }
            }
        }else{
            $resultarray=[
                        'message'=>'error'
                    ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function unlikemoment(){
        $result = Db::name('momentlike')->where('moment_id',input('momentid'))->where('user_id',input('userid'))->delete();
        if($result){
            Db::name('moment')->where('moment_id',input('momentid'))->dec('moment_like')->update();
            $resultarray=[
                'message'=>'success'
            ];
        }else{
            $resultarray=[
                'message'=>'error'
            ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function islikemoment(){
        $result = Db::name('momentlike')->where('moment_id',input('momentid'))->where('user_id',input('userid'))->find();
        if($result){
            $resultarray=[
                'message'=>'success'
            ];
        }else{
            $resultarray=[
                'message'=>'error'
            ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getmylikemoment(){

    }



    public function getnewlist(){
        $resultarray = Db::name('news')->field('news_content,news_intro',true)->where('city_id',input('cityid'))->where('news_status',1)->where('news_images<>"" ')->order('news_time desc')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getnews(){
        $resultarray = Db::name('news')->field('news_content,news_intro',true)->where('news_id',input('newsid'))->find();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }


    public function gethotnewlist(){
        $resultarray = Db::name('news')->field('news_content,news_intro',true)->where('city_id',input('cityid'))->where('news_status',1)->where('news_images<>"" ')->order('news_hot desc')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function addnewsHot(){
        $result = Db::name('news')->where('news_id',input('newsid'))->inc('news_hit')->inc('news_hot')->update();
        if($result){
            $resultarray=[
                'message'=>'success'
            ];
        }else{
            $resultarray=[
                'message'=>'error'
            ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }




    public function searchnew(){
        $resultarray = Db::name('news')->field('news_content,news_intro',true)->where('news_title|news_intro|news_author','like',"%".input('search')."%")->where('city_id',input('cityid'))->where('news_images<>"" ')->where('news_status',1)->order('news_time desc')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getnewscomment(){
        $resultarray = Db::name('newscomment')->where('news_id',input('newsid'))->alias('a')->join('user b','a.user_id = b.user_id')->order('comment_id desc')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function likenewcomment(){
        $data = [
            'comment_id'=>input('commentid'),
            'user_id'=>input('userid'),
            'like_time'=>date('Y-m-d H:i:s'),
        ];
        $result = Db::name('newslike')->insert($data);
        if($result){
            $result2 = Db::name('newscomment')->where('comment_id',input('commentid'))->inc('comment_like')->update();
            $result3 = Db::name('news')->where('news_id',input('newsid'))->inc('news_hot',2)->update();
            if($result2){
                if($result2){
            $resultarray=[
                    'message'=>'success'
                ];
            }else{
                $resultarray=[
                        'message'=>'error'
                    ];
                }
            }
        }else{
            $resultarray=[
                        'message'=>'error'
                    ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function unlikenewcomment(){
        $result = Db::name('newslike')->where('comment_id',input('commentid'))->where('user_id',input('userid'))->delete();
        $result2 = Db::name('newscomment')->where('comment_id',input('commentid'))->dec('comment_like')->update();
        $result3 = Db::name('news')->where('news_id',input('newsid'))->dec('news_hot',2)->update();
        if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function islikenewcomment(){
        $result = Db::name('newslike')->where('comment_id',input('commentid'))->where('user_id',input('userid'))->find();
        if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getlikenewcomment(){
        $resultarray = Db::name('newslike')->field('like_id,b.news_id,b.comment_id,b.comment_like,b.comment_time,b.user_id,user_name,user_headimage')->where('a.user_id',input('userid'))->alias('a')->join('newscomment b','a.comment_id=b.comment_id')->join('user c','b.user_id=c.user_id')->order('a.like_id desc')->where('b.comment_status',1)->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function upnewcomment(){
        $data = [
            'news_id'=>input('newsid'),
            'user_id'=>input('userid'),
            'comment_content'=>input('content'),
            'comment_time'=>date('Y-m-d H:i:s'),
            'comment_like'=>0,
            'comment_status'=>1
        ];
        $result = Db::name('newscomment')->insert($data);
        if($result){
            Db::name('news')->where('news_id',input('newsid'))->inc('news_comment')->inc('news_hot',3)->update();
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function delnewcomment(){
        $result = Db::name('newscomment')->where('comment_id',input('commentid'))->update(['comment_status'=>2]);
        if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function replynewcomment(){
        $data = [
            'news_id'=>input('newsid'),
            'comment_id'=>input('commentid'),
            'user_id'=>input('userid'),
            'reply_content'=>input('content'),
            'reply_time'=>date('Y-m-d H:i:s'),
            'scan_status'=>2,
            'reply_status'=>1
        ];
        $result = Db::name('newsreply')->insert($data);
        if($result){
            Db::name('news')->where('news_id',input('newsid'))->inc('news_comment')->inc('news_hot',3)->update();
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function delreplynewcomment(){
        $result = Db::name('newsreply')->where('reply_id')->update(['reply_status=>2']);
        if($result){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getreplynewcomment(){
        $resultarray = Db::name('newsreply')->where('comment_id',input('commentid'))->alias('a')->join('user b','a.user_id=b.user_id')->order('reply_id desc')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    //查看未读
    public function getreplynewcommentforuser(){
        $result = Db::name('newscomment')->field('comment_id')->where('user_id',input('userid'))->where('comment_status',1)->select();
        $resultarray = array();
        foreach ($result as $value) {
            $result = Db::name('newsreply')->where('comment_id',$value['comment_id'])->where('scan_status',2)->where('reply_status',1)->alias('a')->join('user b','a.user_id=b.user_id')->select();
            if(!empty($result)){
                $resultarray += $result;
            }
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getpostlist(){
        $type = input('type');
        if($type == 1){
            //new
            $resultarray = Db::name('post')->where('a.city_id',input('cityid'))->where('post_status',1)->alias('a')->join('user b','a.user_id=b.user_id')->order('post_id desc')->select();
        }else if($type == 2){
            //hot
            $resultarray = Db::name('post')->where('a.city_id',input('cityid'))->where('post_status',1)->alias('a')->join('user b','a.user_id=b.user_id')->order('post_hot desc')->select();
        }else if($type == 3){
            //commenttime
            $resultarray = Db::name('post')->where('a.city_id',input('cityid'))->where('post_status',1)->alias('a')->join('user b','a.user_id=b.user_id')->order('post_commenttime desc')->select();
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getuserpostlist(){
        $resultarray = Db::name('post')->where('a.user_id',input('userid'))->where('post_status',1)->alias('a')->join('user b','a.user_id=b.user_id')->order('post_id desc')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getpostlistforcommunity(){
        $type = input('type');
        if($type == 1){
            //new
            $resultarray = Db::name('post')->where('a.city_id',input('cityid'))->where('a.community_id',input('communityid'))->where('post_status',1)->whereOr('post_status',4)->alias('a')->join('user b','a.user_id=b.user_id')->order('post_status desc')->order('post_id desc')->select();
        }else if($type == 2){
            //hot
            $resultarray = Db::name('post')->where('a.city_id',input('cityid'))->where('a.community_id',input('communityid'))->where('post_status',1)->whereOr('post_status',4)->alias('a')->join('user b','a.user_id=b.user_id')->order('post_status desc')->order('post_hot desc')->select();
        }else if($type == 3){
            //commenttime
            $resultarray = Db::name('post')->where('a.city_id',input('cityid'))->where('a.community_id',input('communityid'))->where('post_status',1)->whereOr('post_status',4)->alias('a')->join('user b','a.user_id=b.user_id')->order('post_status desc')->order('post_commenttime desc')->select();
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getpost(){
        $resultarray = Db::name('post')->where('a.post_id',input('postid'))->alias('a')->join('user b','a.user_id=b.user_id')->find();
        Db::name('post')->where('post_id',input('postid'))->inc('post_hit')->inc('post_hot')->update();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function uppost(){
        $data=[
          'city_id'=>input('city'),
          'community_id'=>input('community'),
          'user_id'=>input('userid'),
          'post_title'=>input('title'),
          'post_content'=>input('content'),
          'post_time'=>date('Y-m-d H:i:s'),
          'post_commenttime'=>0,
          'post_hit'=>0,
          'post_like'=>0,
          'post_comment'=>0,
          'post_hot'=>0,
          'comment_status'=>1,
          'post_status'=>1,
        ];
        $res=Db::name('post')->insertGetId($data);
        if($res){
            //用户关注该帖子
            Db::name('postfocus')->insert([
                'post_id'=>$res,
                'user_id'=>input('userid'),
                'focus_time'=>date('Y-m-d H:i:s')]);
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }


    public function uppostimgs(){
        $files = request()->file('image');
        $i = 0;
        $resultarray = array();
        foreach($files as $file){
            $info = $file->validate(['size'=>314572800000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters',md5(microtime(true)).$i);
            $resultarray[$i] = [
                'img'=>$info->getFilename()
            ];
            $i++;
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function delpostimgs($data){
        $a = json_decode($data,true);
        $data = array();
        $i=0;
        foreach ($a as $key => $value) {
            $url = $value['img'];
            $filename = ROOT_PATH . 'public' . DS . 'static'. DS .'posters'. DS .$url;
            if(file_exists($filename)){
                unlink($filename);
            }else{
               $resultarray=[
                    'message'=>'error'
                ];
                break;
            }
        }
        $resultarray=[
                    'message'=>'success'
                ];
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function updatepost(){
        $data=[
          'post_title'=>input('title'),
          'post_content'=>input('content'),
        ];
        $res=Db::name('post')->where('post_id',input('postid'))->update($data);
        if($res!==false){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    //隐藏post,删掉全部关注,点赞,隐藏全部评论,无法恢复
    public function delpost(){
        Db::name('post')->where('post_id',input('id'))->update(['post_status'=>2]);
        Db::name('postcomment')->where('post_id',input('id'))->update(['postcomment_status'=>2]);
        Db::name('postfocus')->where('post_id',input('id'))->delete();
        Db::name('postlike')->where('post_id',input('id'))->delete();
        $resultarray=[
                    'message'=>'success'
                ];
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function focuspost(){
        $data = [
            'post_id'=>input('postid'),
            'user_id'=>input('userid'),
            'focus_time'=>date('Y-m-d H:i:s')
        ];
        $res = Db::name('postfocus')->insert($data);
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function unfocuspost(){
        $res = Db::name('postfocus')->where('post_id',input('postid'))->where('user_id',input('userid'))->delete();
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function isfocuspost(){
        $res = Db::name('postfocus')->where('post_id',input('postid'))->where('user_id',input('userid'))->find();
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function likepost(){
        $data = [
            'post_id'=>input('postid'),
            'user_id'=>input('userid'),
            'like_time'=>date('Y-m-d H:i:s')
        ];
        $res = Db::name('postlike')->insert($data);
        if($res){
            Db::name('post')->where('post_id',input('postid'))->inc('post_like')->inc('post_hot',2)->update();
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function unlikepost(){
        $res = Db::name('postlike')->where('post_id',input('postid'))->where('user_id',input('userid'))->delete();
        if($res){
            Db::name('post')->where('post_id',input('postid'))->dec('post_like')->dec('post_hot',2)->update();
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function islikepost(){
        $res = Db::name('postlike')->where('post_id',input('postid'))->where('user_id',input('userid'))->find();
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getpostcomment(){
        $resultarray = Db::name('postcomment')->where('post_id',input('postid'))->alias('a')->join('user b','a.user_id=b.user_id')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getpostcommentreply(){
        $resultarray = Db::name('postreply')->where('comment_id',input('commentid'))->alias('a')->join('user b','a.user_id=b.user_id')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function replypostcomment(){
        $date = date('Y-m-d H:i:s');
        $data = [
            'comment_id'=>input('commentid'),
            'user_id'=>input('userid'),
            'reply_content'=>input('content'),
            'reply_time'=>$date,
            'scan_status'=>2,
            'reply_status'=>1
        ];
        $res = Db::name('postreply')->insert($data);
        if($res){
            Db::name('post')->where('post_id',input('postid'))->inc('post_comment')->inc('post_hot',2)->update();
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function delreplypostcomment(){
        $res = Db::name('postreply')->where('reply_id',input('replyid'))->update(['reply_status'=>2]);
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function uppostcomment(){
        $date = date('Y-m-d H:i:s');
        $data = [
            'post_id'=>input('postid'),
            'user_id'=>input('userid'),
            'comment_content'=>input('content'),
            'comment_time'=>$date,
            'comment_like'=>0,
            'scan_status'=>2,
            'postcomment_status'=>1
        ];
        $res = Db::name('postcomment')->insert($data);
        if($res){
            Db::name('post')->where('post_id',input('postid'))->inc('post_comment')->inc('post_hot',2)->update(['post_commenttime'=>$date,'comment_status'=>2]);
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function delpostcomment(){
        $res = Db::name('postcomment')->where('comment_id',input('commentid'))->update(['postcomment_status'=>2]);
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function searchpost(){
        $resultarray = Db::name('post')->where('a.city_id',input('cityid'))->where('post_status',1)->where('post_content|post_title|b.user_name','like','%'.input('search').'%')->alias('a')->join('user b','a.user_id=b.user_id')->order('post_id desc')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function focususer(){
        $data = [
            'user_id'=>input('userid'),
            'user_fid'=>input('userfid'),
            'focus_time'=>date('Y-m-d H:i:s')
        ];
        $res = Db::name('userfocus')->insert($data);
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function unfocususer(){
        $res = Db::name('userfocus')->where('user_id',input('userid'))->where('user_fid',input('userfid'))->delete();
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function isfocususer(){
        $res = Db::name('userfocus')->where('user_id',input('userid'))->where('user_fid',input('userfid'))->find();
        if($res){
            $resultarray=[
                    'message'=>'success'
                ];
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function updatecity(){

    }

    public function getuser(){
        $user = Db::name('user')->where('user_id',input('userid'))->alias('a')->join('city b','a.city_id = b.city_id')->find();
        $focuscount = Db::name('userfocus')->where('user_id',input('userid'))->count();
        $count1 = [
            'focuscount'=>$focuscount
        ];
        $ffocuscount = Db::name('userfocus')->where('user_fid',input('userid'))->count();
        $count2 = [
            'ffocuscount'=>$ffocuscount
        ];
        $resultarray = array_merge($user,$count1,$count2);
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function forgotuserpassword(){
        $resultarray = array();
        if(request()->isPost()){
                $u = input('email');
                $user = Db::name('user')->where('user_email',$u)->find();
                if(!$user){
                    $resultarray=[
                        'message'=>'邮箱错误'
                    ];
                }else{
                    $checkcode = input('captchacode');
                    $checkid = input('captchaid');
                    $r = Db::name('captcha')->where('captcha_id',$checkid)->find();
                    if(!($r['captcha_code']==$checkcode)){
                        $resultarray=[
                            'message'=>'验证码输入错误'
                        ];
                    }else{
                        $array = array();
                        $array = [
                            'user_password'=>md5(input('password')),
                        ];
                        $result = Db::name('user')->where('user_email',input('email'))->update($array);
                        if($result){
                            $resultarray=[
                                'message'=>'success'
                            ];
                        }else{
                            $resultarray=[
                                'message'=>'密码与之前一致'
                            ];
                        }
                    }
                }  
        }else{
            $resultarray=[
                    'message'=>'error'
                ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getmyfocuslist(){
        $resultarray = Db::name('userfocus')->where('a.user_id',input('userid'))->alias('a')->join('user b','a.user_fid=b.user_id')->select();
        $resultarray2 = Db::name('userfocus')->where('user_fid',input('userid'))->alias('a')->join('user b','a.user_id=b.user_id')->select();
        $array = [
            'focus'=>$resultarray,
            'fans'=>$resultarray2
        ];
        echo json_encode($array,JSON_UNESCAPED_UNICODE);
    }

    public function getmyfocuspostlist(){
        $resultarray3 = Db::name('postfocus')->where('a.user_id',input('userid'))->alias('a')->join('user b','a.user_id=b.user_id')->join('post c','a.post_id = c.post_id')->select();
        echo json_encode($resultarray3,JSON_UNESCAPED_UNICODE);
    }

    public function getmypostlikelist(){
        $resultarray = Db::name('postlike')->where('a.user_id',input('userid'))->alias('a')->join('post b','a.post_id = b.post_id')->join('user c','b.user_id = c.user_id')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function getmymomentlikelist(){
        $resultarray = Db::name('momentlike')->where('a.user_id',input('userid'))->alias('a')->join('moment b','a.moment_id = b.moment_id')->join('user c','b.user_id = c.user_id')->select();
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function undateusername(){
        $result = Db::name('user')->where('user_name',input('name'))->find();
        if(!$result){
            $result1 = Db::name('user')->where('user_id',input('userid'))->update(['user_name'=>input('name')]);
            if($result1!==false){
                $resultarray=[
                    'message'=>'success'
                ];
            }else{
                $resultarray=[
                    'message'=>'服务器错误'
                ];
            }
        }else{
            $resultarray=[
                    'message'=>'昵称重复'
                ];
        }
        
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function updateuserhaedimage(){
        $v = request()->file('image');
        $videoinfo = $v->rule('uniqid')->validate(['size'=>314572800000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
        $image = $videoinfo->getFilename();
        $result = Db::name('user')->where('user_id',input('userid'))->update(['user_headimage'=>$image]);
        if($result){
            $resultarray=[
                'message'=>'success'
            ];
        }else{
            $resultarray=[
                'message'=>'error'
            ];
        }
         echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function updateuserintro(){
        $result = Db::name('user')->where('user_id',input('userid'))->update(['user_intro'=>input('intro')]);
        if($result!==false){
            $resultarray=[
                'message'=>'success'
            ];
        }else{
            $resultarray=[
                'message'=>'error'
            ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    

    public function updateusergender(){
        $result = Db::name('user')->where('user_id',input('userid'))->update(['user_gender'=>input('gender')]);
        if($result!==false){
            $resultarray=[
                'message'=>'success'
            ];
        }else{
            $resultarray=[
                'message'=>'error'
            ];
        }
        echo json_encode($resultarray,JSON_UNESCAPED_UNICODE);
    }

    public function wechatlogin(){

    }

    public function getversion(){
        $sql = "SELECT * FROM `version`order by `version_id` desc limit 1";
        $resultarray = Db::query($sql);
        echo json_encode($resultarray[0],JSON_UNESCAPED_UNICODE);
    }






	
}